//
//  RootViewController_iPad.m
//  PriceOnline
//
//  Created by Le Anh Tai on 8/3/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "RootViewController_iPad.h"
#import "UIConstants.h"
#import "Reachability.h"
@import GoogleMobileAds;

#define PADDING 10

@interface RootViewController_iPad ()

@end

@implementation RootViewController_iPad

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
//    _chartController = [[ChartViewController alloc] initWithNibName:@"ChartViewController" bundle:nil];
//    [self.view addSubview:_chartController.view];

    //goldPrice view
    _goldPriceView = [[UIView alloc]init];
    [self.view addSubview:_goldPriceView];
    
    _goldViewController = [[GoldPriceViewController alloc]initWithNibName:@"GoldPriceViewController" bundle:nil];
    [_goldPriceView addSubview:_goldViewController.view];
    
    _titleGoldPriceView = [FlatToolBarViewItem toolbarItemWithTitle:TEXT_TITLE_GOLD_PRICE
                                                               icon:@"iconGoldPrice.png"
                                                    highlightHeight:1];
    _titleGoldPriceView.sourceLabel.text = TEXT_SOURCE_GOLD_PRICE;
    _titleGoldPriceView.selected = YES;
    
    [_titleGoldPriceView setUserInteractionEnabled:NO];
    [_goldPriceView addSubview:_titleGoldPriceView];
    
    
    //rate view
    _ratesView = [[UIView alloc]init];
    [self.view addSubview:_ratesView];
    
    _ratesViewController = [[RatesViewController alloc] initWithNibName:@"RatesViewController" bundle:nil];
    [_ratesView addSubview:_ratesViewController.view];
    
    _titleRatesView = [FlatToolBarViewItem toolbarItemWithTitle:TEXT_TITLE_EXCHANGE_RATE
                                                           icon:@"iconExchangeRate.png"
                                                highlightHeight:1];
    _titleRatesView.sourceLabel.text = TEXT_SOURCE_EXCHANGE_RATE;
    _titleRatesView.selected = YES;
    
    [_titleRatesView setUserInteractionEnabled:NO];
    [_ratesView addSubview:_titleRatesView];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [self.view bringSubviewToFront:_menuButton];
    [self.view bringSubviewToFront:_menuViewController.view];
}

- (void)reloadLanguage
{
    [_titleGoldPriceView setTitle:TEXT_TITLE_GOLD_PRICE forState:UIControlStateNormal];
    [_titleRatesView setTitle:TEXT_TITLE_EXCHANGE_RATE forState:UIControlStateNormal];
    _titleGoldPriceView.sourceLabel.text = TEXT_SOURCE_GOLD_PRICE;
    _titleRatesView.sourceLabel.text = TEXT_SOURCE_EXCHANGE_RATE;
    
    [_titleGoldPriceView setNeedsLayout];
    [_titleRatesView setNeedsLayout];
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    [self layoutSubviews];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)layoutSubviews
{
    if ([_curAds isKindOfClass:[GADBannerView class]])
    {
        if (Is_Landscape)
        {
            [(GADBannerView *)_curAds setAdSize:kGADAdSizeSmartBannerLandscape];
        }
        else
        {
            [(GADBannerView *)_curAds setAdSize:kGADAdSizeSmartBannerPortrait];
        }
    }
    
    CGFloat contentHeight;
    static NSInteger heightTitle = TOOLBAR_HEIGHT;
    NetworkStatus netStatus = [[Reachability reachabilityForInternetConnection] currentReachabilityStatus];
    NSInteger heightBanner = netStatus == NotReachable? 0: _curAds.height;
    
    NSInteger heightStatusiOS7 = 0;
    if (IS_SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_IOS7)
    {
        heightStatusiOS7 = 20;
    }
    
    if (Is_Landscape)
    {
        int w = self.view.height;
        int h = self.view.width;
        
        // Fix: iOS 8 produces w, h exactly
        if (IS_SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_IOS8)
        {
            w = self.view.width;
            h = self.view.height;
        }
        
        contentHeight = h - heightBanner - heightStatusiOS7;
        
        _goldPriceView.frame = RECT(0, heightStatusiOS7, w/2, contentHeight);
        
        _ratesView.frame = RECT(_goldPriceView.right + PADDING, heightStatusiOS7,
                                w - (_goldPriceView.right + PADDING), contentHeight);
        
        _curAds.origin = CGPointMake((w - _curAds.width)/2, h - heightBanner);
    }
    else
    {
        int w = self.view.width;
        int h = self.view.height;
        
        contentHeight = h - heightBanner - heightStatusiOS7;
        
        _goldPriceView.frame = RECT(0, heightStatusiOS7, w, contentHeight/2);
        _ratesView.frame = RECT(0, _goldPriceView.bottom + PADDING,
                                w, h - _goldPriceView.bottom - heightBanner - PADDING);
        
        _curAds.origin = CGPointMake((w - _curAds.width)/2, h - heightBanner);
    }
    
    _goldViewController.view.frame = RECT(0, heightTitle, _goldPriceView.width, _goldPriceView.height - heightTitle);
    _titleGoldPriceView.frame = RECT(0, 0, _goldPriceView.width, heightTitle);
    
    _ratesViewController.view.frame = RECT(0, heightTitle, _ratesView.width, _ratesView.height - heightTitle);
    _titleRatesView.frame = RECT(0, 0, _ratesView.width, heightTitle);
    
    if (Is_Landscape)
    {
        [_titleRatesView showTempStatusBarView];
    }
    else
    {
        [_titleRatesView hideTempStatusBarView];
    }
    
    _menuButton.frame = RECT(0, heightStatusiOS7, 40, _titleGoldPriceView.height);
    
    _menuViewController.view.frame = self.view.bounds;
    [_menuViewController layoutSubviews];
}

#pragma mark - Rotation methods
// This method was deprecated in iOS 6
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Overriden to allow any orientation.
    return YES;
}

#pragma mark - Orientation in iOS 6.0
// Apple replaced shouldAutorotateToInterfaceOrientation method with 02 other methods: supportedInterfaceOrientations, shouldAutorotate
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [self layoutSubviews];
}

@end
